package com.seafwg.dao.impl;

import com.seafwg.dao.UserDao;
import com.seafwg.domain.User;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @Author seafwg
 * @Date 2022/3/4 3:40 下午
 * @Version 1.0
 * @description xxx
 */

public class UserDaoImpl implements UserDao {
    // 注入jdbcTemplate[使用set注入JdbcTemplate]
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    // 用户登陆
    @Override
    public User findUserByLoginInfo(String username, String password) {
        User user = jdbcTemplate.queryForObject(
                "select * from sys_user where username=? and password=?",
                new BeanPropertyRowMapper<>(User.class),
                username,
                password
        );
        return user;
    }

    @Override
    public List<User> findAll() throws EmptyResultDataAccessException {
        List<User> userList = jdbcTemplate.query("select * from sys_user", new BeanPropertyRowMapper<User>(User.class));
        return userList;
    }
}
